#!/bin/bash

CID=$(nerdctl -n k8s.io ps | grep nm | grep -v '/pause' | awk '{print $1}')

for c in ${CID}
do

pod=$(nerdctl -n k8s.io inspect "${c}" | grep 'pod.uid' | awk -F ':' '{print $2}' | sed 's/[" ,]//g')

dir=$(mount | grep "${pod}.*export" | awk '{print $3}')
ips=$(nerdctl -n k8s.io exec ${c} ip a | grep 'eth0@' -A3 | grep inet | awk '{print $2}')
ip=${ips%%/*}

pid=$(nerdctl -n k8s.io top ${c} | grep 'executor-id.*__app__.jar$' | awk '{print $2}' | head -n 1)

if [ -z "${pid}" ]; then
   exit 0
fi

l=$(mktemp)
perf stat -p ${pid} -- sleep 10 &> ${l}

pl=${dir}/perf_stat.log
echo "log to: ${pl}"
echo "ip ${ip}" > ${pl}
cat ${l} | grep '#' | awk '{print $2, $1}' | tail -n +2 >> ${pl}
\rm -f ${l}
done
